package com.sssp.handler;

import com.sssp.entities.Employee;
import com.sssp.service.DepartmentService;
import com.sssp.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2018/11/2 11:04
 */
@Controller
public class EmployeeController {
    private static final int PAGE_SIZE = 5;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DepartmentService departmentService;

    /**
     * 列出全部的员工信息，带有分页的效果
     *
     * @param pageNoStr
     * @param map
     * @return
     */
    @RequestMapping("/emps")
    public String List(@RequestParam(value = "pageNo", required = false, defaultValue = "1") String pageNoStr, Map<String, Object> map) {
        int pageNo = 1;
        try {
            pageNo = Integer.parseInt(pageNoStr);
            if (pageNo < 1) {
                pageNo = 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        Page<Employee> page = employeeService.getPage(pageNo, PAGE_SIZE);
        map.put("page", page);

        return "emp/list";
    }

    /**
     * 跳转到输入新员工页面
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/emp", method = RequestMethod.GET)
    public String input(Map<String, Object> map) {
        map.put("departments", departmentService.getAll());
        map.put("employee", new Employee());
        return "emp/input";
    }

    /**
     * 配合ajax接口实现用户名验证
     *
     * @param lastName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/ajaxValidateLastName", method = RequestMethod.POST)
    public String validateLastName(@RequestParam(value = "lastName", required = true) String lastName) {
        Employee employee = employeeService.getEmployeeByLastName(lastName);
        if (employee == null) {
            return "0";
        } else {
            return "1";
        }
    }

    /**
     * 保存Employee
     *
     * @param employee
     * @return
     */
    @RequestMapping(value = "/emp", method = RequestMethod.POST)
    public String save(Employee employee) {
        employeeService.save(employee);
        return "redirect:/emps";
    }

    /**
     * 保存修改后的员工信息
     *
     * @param employee
     * @return
     */
    @RequestMapping(value = "/emp", method = RequestMethod.PUT)
    public String update(Employee employee) {
        employeeService.save(employee);
        return "redirect:/emps";
    }

    /**
     * 跳转到修改员工信息的页面，并对表单进行回显
     * 若id不为空，则说明需要回显，此时在请求域中加入从数据库查询到的员工信息进行回显
     *
     * @param id
     * @param map
     */
    @RequestMapping("emp/{id}")
    public String input(@PathVariable(name = "id") Integer id, Map<String, Object> map) {
        Employee employee = employeeService.getEmployById(id);
        map.put("employee", employee);
        map.put("departments", departmentService.getAll());
        return "emp/input";
    }

    @ModelAttribute
    public void getEmployee(@RequestParam(value = "id", required = false) Integer id, Map<String, Object> map) {
        if (id != null) {
            Employee employee = employeeService.getEmployById(id);
            employee.setDepartment(null);
            map.put("employee", employee);
        }
    }

    @RequestMapping(value = "/emp/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable(name = "id") Integer id) {
        employeeService.delete(id);
        return "redirect:/emps";
    }

}
